package pub.wkq.work.mq;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import pub.wkq.api.mq.InitResumeMQConfig;
import pub.wkq.common.utils.SerializationUtils;
import pub.wkq.work.service.ResumeService;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Objects;

/**
 * 初始化简历创建的消费者
 */
@Slf4j
@Component
public class InitResumeMQConsumer {
    @Resource
    private ResumeService resumeService;
    @RabbitListener(queues = {InitResumeMQConfig.INIT_RESUME_QUEUE})
    public void receive(Message msg, Channel channel) {
        log.info("接收到的消息是：{}", msg);
        String routingKey = msg.getMessageProperties().getReceivedRoutingKey();
        String userId = new String(msg.getBody());
        if (Objects.equals(InitResumeMQConfig.INIT_RESUME_ROUTING_KEY, routingKey)) {
            //手动确认消息
            /**
             * 参数一：消息的唯一标识
             * 参数二：是否批量确认，true：批量确认所有小于当前消息唯一标识的消息
             *       false：只确认当前消息
             *       如果为true，会将当前消息之前的所有消息都确认
             */
            try {
                resumeService.initResume(SerializationUtils.deserializeObject(userId, String.class));
                channel.basicAck(msg.getMessageProperties().getDeliveryTag(), false);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
